package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.order.CartService;
import com.qingcheng.service.order.OrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Title: CartController
 * @Description:
 * @Auther:
 * @Version: 1.0
 * @create 2021/10/12 9:14
 */
@RestController
@RequestMapping("cart")
public class CartController {

    @Reference
    private CartService cartService;


    /**
     * 展示xxx购物车
     * @return
     */
    @RequestMapping("findCart")
    public List<Map<String, Object>>  findCart(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        return cartService.findCart(name);
    }

    /**
     *  加入商品到购物车 第一次
     *  修改购物车的数量
     *  删除购物车的数据
     *
     * @param skuId
     * @param num
     * @return
     */
    @RequestMapping("addCartItem")
    public Result addCartItem(String skuId,Integer num){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addCartItem(name,skuId,num);
        return new Result();
    }

    @RequestMapping("addCartItem2")
    public void addCartItem2(HttpServletResponse response,String skuId, Integer num){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.addCartItem(name,skuId,num);
        //重定向到购物车页面
        try {
            response.sendRedirect("/cart.html");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 更新购物车项前面的复选框的状态
     * @param skuId
     * @param checked
     * @return
     */
    @RequestMapping("updateCartItemChecked")
    public Result updateCartItemChecked(String skuId,Boolean checked){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.updateCartItemChecked(name,skuId,checked);
        return new Result();
    }


    @RequestMapping("deleteCheckedCartItems")
    public Result deleteCheckedCartItems(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        cartService.deleteCheckedCartItems(name);
        return new Result();
    }


    /**
     * 获取某个人购物商品优惠的价格
     * @return
     */
    @RequestMapping("getPreferentialMoney")
    public Integer getPreferentialMoney(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
       return  cartService.getPreferentialMoney(name);
    }


    /**
     * 获取某个人要购买的商品清单列表
     * @return
     */
    @RequestMapping("getBuyOrderItems")
    public List<OrderItem> getBuyOrderItems(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
       return  cartService.getBuyOrderItems(name);
    }

}
